//define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','bootstrap-daterangepicker'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/index' + location.search,
                    add_url: 'statistics/add',
                    edit_url: 'statistics/edit',
                    del_url: 'statistics/del',
                    multi_url: 'statistics/multi',
                    import_url: 'statistics/import',
                    table: 'statistics',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        /*{field: 'id', title: __('Id')},*/
                        {field: 'date', title: __('Date'),sortable: true, operate:'RANGE', addclass:'datetimerange', data:'data-date-format="YYYY-MM-DD"', autocomplete:false},
                        {field: 'app_id', title: __('应用名'),addclass: 'selectpage', extend:'data-source="ajax/getApp" ',visible:false,defaultValue:'11'},
                        {field: 'app.name', title: __('应用名'), operate: false},

                        {field: 'new', title: __('New'),sortable: true,operate: false},
                        {field: 'login', title: __('Login'),sortable: true,operate: false},
                        /*{field: 'sdk', title: __('Sdk')},
                        {field: 'sign', title: __('Sign')},
                        {field: 'double_sign', title: __('Double_sign')},
                        {field: 'apply', title: __('Apply')},
                        {field: 'adopt', title: __('Adopt')},
                        {field: 'click_coin', title: __('Click_coin')},
                        {field: 'click_dollar', title: __('Click_dollar')},
                        {field: 'click_cashout', title: __('Click_cashout')},*/
                        {field: 'advert', title: __('当日广告播放总次数'),sortable: true,operate: false},
                        /*{field: 'advert_fill', title: __('Advert_fill'), operate:'BETWEEN'},*/
                        {field: 'advert1', title: __('激励播放次数'),sortable: true,operate: false},
                        /*{field: 'advert1_fill', title: __('Advert1_fill'), operate:'BETWEEN'},*/
                        {field: 'advert2', title: __('插屏播放次数'),sortable: true,operate: false},
                        /*{field: 'advert2_fill', title: __('Advert2_fill'), operate:'BETWEEN'},*/
                        {field: 'advert3', title: __('banner播放次数'),sortable: true,operate: false},
                       /* {field: 'advert3_fill', title: __('Advert3_fill'), operate:'BETWEEN'},*/
                        {field: 'advert4', title: __('积分墙播放次数'),sortable: true,operate: false},
                        /*{field: 'advert4_fill', title: __('Advert4_fill'), operate:'BETWEEN'},*/

                       /* {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            /*$(".toggle1").addClass("toggle1 active");
            $(".toggle2").click(function(){
                Backend.api.addtabs('statistics/echarts','折线图');
                //window.location.href = "user_keep_day/echarts?addtabs=1";
            });*/
        },
        echarts: function () {
            //时间插件
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD HH:mm:ss',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });
            /* var legend = [];
             var xAxis = [];
             var series = [];*/
            var field = $('#c-flag option:selected').val();
            var startDay = $('input[name="startDay"]').val();
            var endDay = $('input[name="endDay"]').val();
            loadEcharts(startDay, endDay, field);
            $('.btn-warning').click(function (){
                loadEcharts($('input[name="startDay"]').val(), $('input[name="endDay"]').val(), $('#c-flag option:selected').val());
            });
            function loadEcharts(startDay, endDay, field) {
                $.ajax({
                    async: false,
                    url: 'statistics/echarts',
                    type: 'get',
                    data: {startDay: startDay, field: field, endDay: endDay},
                    dataType: 'json',
                    success: function (res) {
                        if (res) {
                            /*console.log(res);
                            legend = res.legend;
                            xAxis = res.xAxis;
                            series = res.series;*/
                            var myChart = Echarts.init(document.getElementById('keep'), 'walden');

                            // 指定图表的配置项和数据
                            option = {
                                title: {
                                    text: '用户新增留存率'
                                },
                                tooltip: {
                                    trigger: 'axis'
                                },
                                legend: {
                                    //data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
                                    data: res.legend
                                },
                                grid: {
                                    left: '3%',
                                    right: '4%',
                                    bottom: '3%',
                                    containLabel: true
                                },
                                toolbox: {
                                    feature: {
                                        saveAsImage: {}
                                    }
                                },
                                xAxis: {
                                    type: 'category',
                                    boundaryGap: false,
                                    //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                                    data: res.xAxis
                                },
                                yAxis: {
                                    type: 'value'
                                },
                                series:res.series,
                            };

                            option && myChart.setOption(option);

                            //console.log(series);
                        } else {
                            Toastr.error('服务器繁忙请稍后再试');
                        }
                    }
                });
            }

            /*$(".toggle2").addClass("toggle1 active");
            $(".toggle1").click(function(){
                Backend.api.addtabs('statistics/index','表格');
                //window.location.href = 'user_keep_day/index';
            })*/


            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    myChart.resize();
                }, 0);
            });
            Controller.api.bindevent();
        },
        action: function () {
            Table.api.init({
                extend: {
                    index_url: 'statistics/action' + location.search,
                    add_url: 'statistics/add',
                    edit_url: 'statistics/edit',
                    del_url: 'statistics/del',
                    multi_url: 'statistics/multi',
                    import_url: 'statistics/import',
                    table: 'statistics',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        /*{field: 'id', title: __('Id')},*/
                        {field: 'date', title: __('Date'),sortable: true, operate:'RANGE', addclass:'datetimerange', data:'data-date-format="YYYY-MM-DD"',autocomplete:false},
                        {field: 'app_id', title: __('应用名'),addclass: 'selectpage', extend:'data-source="ajax/getApp" ',visible:false,defaultValue:'11'},
                        {field: 'app.name', title: __('应用名'), operate: false},
                        /*{field: 'new', title: __('New'),sortable: true,operate: false},*/
                        {field: 'login', title: __('活跃用户人数'),sortable: true,operate: false},
                        {field: 'sdk', title: __('进入sdk人数'),sortable: true,operate: false},
                        {field: 'sign', title: __('签到人数'),sortable: true,operate: false},
                        {field: 'double_sign', title: __('双倍签到人数'),sortable: true,operate: false},
                        {field: 'apply', title: __('paypal验证申请人数'),sortable: true,operate: false},
                        {field: 'adopt', title: __('paypal验证通过人数'),sortable: true,operate: false},
                        {field: 'click_coin', title: __('Cash out点击次数'),sortable: true,operate: false},
                        {field: 'click_dollar', title: __('Redeem点击次数'),sortable: true,operate: false},
                        {field: 'click_cashout', title: __('提现申请数'),sortable: true,operate: false},
                        /*{field: 'advert', title: __('Advert')},
                        {field: 'advert_fill', title: __('Advert_fill'), operate:'BETWEEN'},
                        {field: 'advert1', title: __('Advert1')},
                        {field: 'advert1_fill', title: __('Advert1_fill'), operate:'BETWEEN'},
                        {field: 'advert2', title: __('Advert2')},
                        {field: 'advert2_fill', title: __('Advert2_fill'), operate:'BETWEEN'},
                        {field: 'advert3', title: __('Advert3')},
                        {field: 'advert3_fill', title: __('Advert3_fill'), operate:'BETWEEN'},
                        {field: 'advert4', title: __('Advert4')},
                        {field: 'advert4_fill', title: __('Advert4_fill'), operate:'BETWEEN'},
                        {field: 'app.name', title: __('App.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}*/
                    ]
                ]
            });
            /*$(".toggle1").addClass("toggle1 active");
            $(".toggle2").click(function(){
                Backend.api.addtabs('statistics/actionecharts','折线图');
                //window.location.href = "user_keep_day/echarts?addtabs=1";
            })*/
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        actionecharts: function () {
            //时间插件
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD HH:mm:ss',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });
            /* var legend = [];
             var xAxis = [];
             var series = [];*/
            var field = $('#c-flag option:selected').val();
            var startDay = $('input[name="startDay"]').val();
            var endDay = $('input[name="endDay"]').val();
            loadEcharts(startDay, endDay, field);
            $('.btn-warning').click(function (){
                loadEcharts($('input[name="startDay"]').val(), $('input[name="endDay"]').val(), $('#c-flag option:selected').val());
            });
            function loadEcharts(startDay, endDay, field) {
                $.ajax({
                    async: false,
                    url: 'statistics/actionecharts',
                    type: 'get',
                    data: {startDay: startDay, field: field, endDay: endDay},
                    dataType: 'json',
                    success: function (res) {
                        if (res) {
                            /*console.log(res);
                            legend = res.legend;
                            xAxis = res.xAxis;
                            series = res.series;*/
                            var myChart = Echarts.init(document.getElementById('keep'), 'walden');

                            // 指定图表的配置项和数据
                            option = {
                                title: {
                                    text: '用户新增留存率'
                                },
                                tooltip: {
                                    trigger: 'axis'
                                },
                                legend: {
                                    //data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
                                    data: res.legend
                                },
                                grid: {
                                    left: '3%',
                                    right: '4%',
                                    bottom: '3%',
                                    containLabel: true
                                },
                                toolbox: {
                                    feature: {
                                        saveAsImage: {}
                                    }
                                },
                                xAxis: {
                                    type: 'category',
                                    boundaryGap: false,
                                    //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                                    data: res.xAxis
                                },
                                yAxis: {
                                    type: 'value'
                                },
                                series:res.series,
                            };

                            option && myChart.setOption(option);

                            //console.log(series);
                        } else {
                            Toastr.error('服务器繁忙请稍后再试');
                        }
                    }
                });
            }

            /*$(".toggle2").addClass("toggle1 active");
            $(".toggle1").click(function(){
                Backend.api.addtabs('statistics/action','表格');
                //window.location.href = 'user_keep_day/index';
            })*/


            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    myChart.resize();
                }, 0);
            });
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});